package cn.sc.pai.test;

import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * 使用ViewDragHelper
 * description：
 * author：pany
 * on 2018/9/19 11:05
 */
public class SwipebackDragActivity extends AppCompatActivity implements SwipeBackDragLayout.OnSwipeListener{

    private final ViewGroup.LayoutParams swipeBackParams =
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    protected SwipeBackDragLayout swipeBackLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().getDecorView().setBackgroundDrawable(null);
        swipeBackLayout = new SwipeBackDragLayout(this);
        swipeBackLayout.setLayoutParams(swipeBackParams);
        swipeBackLayout.setOnSwipeListener(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        TypedArray a = getTheme().obtainStyledAttributes(new int[]{
                android.R.attr.windowBackground
        });
        int background = a.getResourceId(0, 0);
        a.recycle();
        view.setBackgroundResource(background);
        swipeBackLayout.addView(view);
        super.setContentView(swipeBackLayout);
    }

    @Override
    public void setContentView(View view) {
        TypedArray a = getTheme().obtainStyledAttributes(new int[]{
                android.R.attr.windowBackground
        });
        int background = a.getResourceId(0, 0);
        a.recycle();
        view.setBackgroundResource(background);
        swipeBackLayout.addView(view);
        super.setContentView(swipeBackLayout);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        TypedArray a = getTheme().obtainStyledAttributes(new int[]{
                android.R.attr.windowBackground
        });
        int background = a.getResourceId(0, 0);
        a.recycle();
        view.setBackgroundResource(background);
        swipeBackLayout.addView(view, params);
        super.setContentView(swipeBackLayout);
    }


    @Override
    public void onSwipeFinish() {
        finish();
    }
}
